﻿
    using Maticsoft.BLL;
    using Maticsoft.DBUtility;
    using Maticsoft.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Utils;
    using Wuqi.Webdiyer;

    public partial class WebPage_DingDan_YiFaHuo : OrderBBasePage
    {
        protected string beginTime = "";
        public Maticsoft.BLL.Card cardbll = new Maticsoft.BLL.Card();
        public Maticsoft.Model.Card cardmodel = new Maticsoft.Model.Card();
        public Maticsoft.BLL.Coupon coupbll = new Maticsoft.BLL.Coupon();
        public Maticsoft.Model.Coupon coupmodel = new Maticsoft.Model.Coupon();
        public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
        public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();
        public PagedDataSource ds = new PagedDataSource();

        public Maticsoft.Model.Flow flow = new Maticsoft.Model.Flow();
        public Maticsoft.BLL.Flow flowbll = new Maticsoft.BLL.Flow();
      
        public int j = 0;
        protected string lasttime = "";

        public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
        public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
        public int pagenum = 0;
        private bool result = false;
        protected string strTeamid = "";
        public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
        public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
        public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
        public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
        public string title = "已发货";
        public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
        public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.pagenum = 0;
            this.getDingdan();
        }

        private void getDingdan()
        {
            this.xiazai();
        }

        public string getTime()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=''></option>");
            for (int i = 0; i < 0x18; i++)
            {
                builder.Append(string.Concat(new object[] { "<option value='", i, "'>", i, "</option>" }));
            }
            return builder.ToString();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Page.Header.Controls.Add(new LiteralControl("<script src='" + base.WebRoot + "upfile/js/datePicker/WdatePicker.js'></script>\r\n"));
            this.HeaderControl1.Select = "order";
            if (!((base.Request["id"] == null) || this.Page.IsPostBack))
            {
                this.UpdadteFlow(Convert.ToInt32(base.Request["id"].ToString()));
                this.Updateorder(Convert.ToInt32(base.Request["id"].ToString()));
            }
            if (base.Request["pagenum"] != null)
            {
                if (Utility.IsNum(base.Request["pagenum"].ToString()))
                {
                    this.pagenum = Convert.ToInt32(base.Request["pagenum"]);
                }
                else
                {
                    this.SetError("友情提示：参数非法");
                }
            }
            if (!this.Page.IsPostBack)
            {
                this.Session.Remove("Type");
                if ((this.Session["teamid"] != null) && (this.Session["teamid"].ToString() != ""))
                {
                    this.strTeamid = this.Session["teamid"].ToString();
                }
                this.getDingdan();
                this.Session.Remove("beginTime");
                this.Session.Remove("lastTime");
            }
            if (base.Request["btnselect"] == "筛选")
            {
                try
                {
                    this.Session["teamid"] = base.Request.Form["teamid"];
                    this.setSqlWhere();
                    this.getDingdan();
                }
                catch (Exception)
                {
                    this.SetError("参数错误，请重新输入");
                }
            }
        }

        public void setSqlWhere()
        {
            DateTime time;
            DateTime time2;
            string text = this.firstTime.Text;
            string str2 = this.endTime.Text;
            string str3 = string.Empty;
            if (str3 != string.Empty)
            {
                this.sqlWhere = str3.Substring(4);
            }
            if (text != "")
            {
                try
                {
                    time = Convert.ToDateTime(text);
                }
                catch (Exception)
                {
                    this.SetError("您输入的时间格式不正确");
                }
            }
            if (str2 != "")
            {
                try
                {
                    time2 = Convert.ToDateTime(str2);
                }
                catch (Exception)
                {
                    this.SetError("您输入的时间格式不正确");
                }
            }
            if ((text != "") && (str2 != ""))
            {
                try
                {
                    time = Convert.ToDateTime(text);
                    time2 = Convert.ToDateTime(str2);
                }
                catch (Exception)
                {
                    this.SetError("您输入的时间格式不正确");
                }
                if ((base.Request.Form["time"] != null) && (base.Request.Form["time"].ToString() != ""))
                {
                    str3 = str3 + " and  (pay_time BETWEEN '" + text + " " + base.Request.Form["time"].ToString() + ":00:00' and '" + str2 + " " + base.Request.Form["time"].ToString() + ":59:59'  )  ";
                }
                else
                {
                    str3 = str3 + " and  (pay_time BETWEEN '" + text + " 00:00:00' and '" + str2 + " 23:59:59'  )  ";
                }
            }
            if ((((this.ddlstate.SelectedValue != null) && (this.txtcontent.Text != null)) && (this.ddlstate.SelectedValue.ToString() != "")) && (this.txtcontent.Text.ToString() != ""))
            {
                switch (this.ddlstate.SelectedValue.ToString())
                {
                    case "1":
                        this.Session.Add("Type", false);
                        str3 = str3 + " and Username like '" + this.txtcontent.Text.ToString().Trim() + "%' ";
                        break;

                    case "2":
                        this.Session.Add("Type", false);
                        str3 = str3 + " and Email like '" + this.txtcontent.Text.ToString().Trim() + "%' ";
                        break;

                    case "3":
                        this.Session.Add("Type", false);
                        try
                        {
                            str3 = str3 + " and [order].Id=" + Convert.ToInt32(this.txtcontent.Text.ToString());
                        }
                        catch (Exception)
                        {
                            this.SetError("订单编号输入格式错误");
                        }
                        break;

                    case "4":
                        try
                        {
                            this.Session.Add("Type", true);
                            str3 = string.Concat(new object[] { str3, " and ((teamid=", Convert.ToInt32(this.txtcontent.Text.ToString()), ") or (team_id=", Convert.ToInt32(this.txtcontent.Text.ToString()), ")) " });
                        }
                        catch (Exception)
                        {
                            this.SetError("项目编号输入格式错误");
                        }
                        break;

                    case "5":
                        try
                        {
                            this.Session.Add("Type", false);
                            str3 = str3 + " and Pay_id like '" + this.txtcontent.Text.ToString().Substring(0, this.txtcontent.Text.ToString().LastIndexOf("as")) + "%' ";
                        }
                        catch (Exception)
                        {
                            this.SetError("交易单号输入格式错误");
                            base.Response.Redirect(base.Request.Url.ToString());
                            base.Response.End();
                            return;
                        }
                        break;

                    case "6":
                        this.Session.Add("Type", false);
                        str3 = str3 + " and Express_no='" + this.txtcontent.Text.ToString().Trim() + "' ";
                        break;

                    case "7":
                        this.Session.Add("Type", false);
                        str3 = str3 + " and [order].Mobile like '" + this.txtcontent.Text.ToString().Trim() + "%' ";
                        break;
                }
            }
            this.sqlWhere = " 1=1 and Express='Y' and Express_id>0 and state='pay' and len(Express_no)>0 " + str3;
        }

        public void UpdadteFlow(int id)
        {
            this.ordermodel = this.orderbll.GetModel(id);
            if (HttpContext.Current.User.Identity.Name != null)
            {
                this.flow.User_id = this.ordermodel.User_id;
                this.flow.Admin_id = this.userbll.Getuserid(HttpContext.Current.User.Identity.Name.ToString());
                this.flow.Detail_id = id.ToString();
                this.flow.Direction = "expense";
                this.flow.Money = this.ordermodel.Origin;
                this.flow.Action = "cash";
                this.flow.Create_time = DateTime.Now;
                this.flowbll.Add(this.flow);
            }
            else
            {
                this.SetError("支付失败,请重新登录");
            }
        }

        public void Updateorder(int id)
        {
            this.ordermodel = this.orderbll.GetModel(id);
            if (this.ordermodel != null)
            {
                int num2;
                int num3;
                int num4;
                this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
                if (this.teammodel != null)
                {
                    Maticsoft.BLL.Order.UpdateNumber(this.ordermodel.Team_id, this.ordermodel.Quantity);
                    this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
                    if ((this.teammodel.Delivery == "coupon") && Maticsoft.BLL.Team.istuan(Convert.ToInt32(this.teammodel.Now_number.ToString()), Convert.ToInt32(this.teammodel.Min_number)))
                    {
                        for (int i = 0; i < this.ordermodel.Quantity; i++)
                        {
                            this.coupmodel.Team_id = this.ordermodel.Team_id;
                            this.coupmodel.Order_id = id;
                            this.coupmodel.Type = "consume";
                            this.coupmodel.Credit = this.teammodel.Credit;
                            this.coupmodel.Consume = "N";
                            this.coupmodel.User_id = this.ordermodel.User_id;
                            this.coupmodel.Create_time = DateTime.Now;
                            this.coupmodel.Expire_time = this.teammodel.Expire_time;
                            this.coupmodel.Partner_id = this.teammodel.Partner_id;
                            this.coupmodel.Id = Helper.GetRandomString(12);
                            this.coupmodel.Secret = Utility.GetCouponpass();
                            this.coupmodel.Sms++;
                            this.coupbll.Add(this.coupmodel);
                            num2 = this.teammodel.Min_number;
                            num3 = this.teammodel.Max_number;
                            num4 = this.teammodel.Now_number;
                            if ((num4 >= num3) && !(this.teammodel.Close_time.HasValue && !(this.teammodel.Close_time.ToString() == "")))
                            {
                                this.teambll.updateValue(this.ordermodel.Team_id.ToString(), "Close_time", DateTime.Now.ToString());
                            }
                            if ((num4 >= num2) && !(this.teammodel.Reach_time.HasValue && !(this.teammodel.Reach_time.ToString() == "")))
                            {
                                this.teambll.updateValue(this.ordermodel.Team_id.ToString(), "Reach_time", DateTime.Now.ToString());
                            }
                            Maticsoft.BLL.Order.UpdateNumber(this.ordermodel.Team_id, this.ordermodel.Quantity);
                            this.sysmodel = this.sysbll.GetModelByCache(1);
                            List<string> mobiles = new List<string>();
                            this.usermodel = this.userbll.GetModel(this.ordermodel.User_id);
                            if (this.usermodel != null)
                            {
                                mobiles.Add(this.usermodel.Mobile);
                                string content = base.ASSystem.abbreviation + "项目：" + this.teammodel.Product + "，编号：" + this.coupmodel.Id + "，密码：" + this.coupmodel.Secret + "，有效期至：" + this.coupmodel.Expire_time.ToString("yyyy年MM月dd日");
                                Maticsoft.BLL.Order.SendSMS(mobiles, content);
                            }
                        }
                    }
                }
                else
                {
                    this.detaillist = this.detailbll.Searchorderdetail(this.ordermodel.Id);
                    foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                    {
                        Maticsoft.BLL.Order.UpdateNumber(Convert.ToInt32(orderdetail.Teamid), Convert.ToInt32(orderdetail.Num));
                        this.teammodel = this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid));
                        num2 = this.teammodel.Min_number;
                        num3 = this.teammodel.Max_number;
                        num4 = this.teammodel.Now_number;
                        if ((num4 >= num3) && !(this.teammodel.Close_time.HasValue && !(this.teammodel.Close_time.ToString() == "")))
                        {
                            this.teambll.updateValue(orderdetail.Teamid.ToString(), "Close_time", DateTime.Now.ToString());
                        }
                        if ((num4 >= num2) && !(this.teammodel.Reach_time.HasValue && !(this.teammodel.Reach_time.ToString() == "")))
                        {
                            this.teambll.updateValue(orderdetail.Teamid.ToString(), "Reach_time", DateTime.Now.ToString());
                        }
                    }
                }
                this.ordermodel.State = "pay";
                this.ordermodel.Service = "cash";
                this.ordermodel.Admin_id = base.AsUser.Id;
                this.ordermodel.Pay_time = new DateTime?(DateTime.Now);
                this.orderbll.Update(this.ordermodel);
            }
            this.getDingdan();
        }

        private void xiazai()
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            builder.Append("<tr >");
            builder.Append("<th width='40'>ID</th>");
            builder.Append("<th class='xiangmu'>项目</th>");
            builder.Append("<th width='70'>用户</th>");
            builder.Append("<th width='30' nowrap>数量</th>");
            builder.Append("<th width='50' nowrap>总款</th>");
            builder.Append("<th width='50' nowrap>支付方式</th>");
            builder.Append("<th width='50' nowrap>余付</th>");
            builder.Append("<th width='50' nowrap>支付</th>");
            builder.Append("<th width='50' nowrap>递送</th>");
            builder.Append("<th width='30' nowrap>操作</th>");
            builder.Append("</tr>");
            Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
            Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
            if (this.pagenum == 0)
            {
                this.pagenum = this.AspNetPager1.CurrentPageIndex;
            }
            List<Maticsoft.Model.Order> list = new List<Maticsoft.Model.Order>();
            Maticsoft.BLL.Order order2 = new Maticsoft.BLL.Order();
            Maticsoft.BLL.Team team = new Maticsoft.BLL.Team();
            Maticsoft.Model.Team model = new Maticsoft.Model.Team();
            string tablename = "";
            Pagers pagers = null;
            if (this.Session["Type"] != null)
            {
                if (Convert.ToBoolean(this.Session["Type"].ToString()))
                {
                    tablename = "[order] left join [orderdetail] on([order].id=[orderdetail].order_id)";
                    pagers = DbHelperSQL.SelectByPager("[order].*", this.sqlWhere, "pay_time desc", this.pagenum, this.AspNetPager1.PageSize, tablename, "", "");
                }
                else
                {
                    tablename = "[order] left join [user] on [order].user_id=[user].id";
                    pagers = DbHelperSQL.SelectByPager("[order].*", this.sqlWhere, "pay_time desc", this.pagenum, this.AspNetPager1.PageSize, tablename, "", "");
                }
                this.Session.Add("strTable", tablename);
            }
            else if (this.Session["strTable"] != null)
            {
                pagers = DbHelperSQL.SelectByPager("[order].*", this.sqlWhere, "pay_time desc", this.pagenum, this.AspNetPager1.PageSize, this.Session["strTable"].ToString(), "", "");
            }
            else
            {
                pagers = DbHelperSQL.SelectByPager("*", this.sqlWhere, "pay_time desc", this.pagenum, this.AspNetPager1.PageSize, "[order]", "", "");
            }
            if (pagers.MyData.Rows.Count > 0)
            {
                int num = 0;
                for (int i = 0; i < pagers.MyData.Rows.Count; i++)
                {
                    DataRow row = pagers.MyData.Rows[i];
                    if ((num % 2) != 0)
                    {
                        builder2.Append("<tr>");
                    }
                    else
                    {
                        builder2.Append("<tr class='alt'>");
                    }
                    builder2.Append("<td>" + row["Id"] + "</td>");
                    this.ordermodel = order2.GetModel(Convert.ToInt32(row["Id"].ToString()));
                    if (this.ordermodel != null)
                    {
                        model = team.GetModel(this.ordermodel.Team_id);
                        if (model != null)
                        {
                            builder2.Append(string.Concat(new object[] { "<td>", model.Id, "&nbsp;(<a class='deal-title' href='/team.aspx?id=", model.Id, "' target='_blank'>", model.Title, "<font style='color:red'>", Maticsoft.BLL.Order.Getbulletin(this.ordermodel.Result), "</font></a>)</td>" }));
                        }
                        else
                        {
                            this.detaillist = this.detailbll.Searchorderdetail(int.Parse(row["Id"].ToString()));
                            builder2.Append("<td>&nbsp;");
                            int num3 = 0;
                            foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                            {
                                num3++;
                                builder2.Append(string.Concat(new object[] { "(<a class='deal-title' href='../team.aspx?id=", orderdetail.Teamid, "' target='_blank'>", num3, ":" }));
                                builder2.Append((this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)) == null) ? "" : string.Concat(new object[] { this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)).Title, "<font style='color:red'>", Maticsoft.BLL.Order.Getbulletin(orderdetail.Result), "</font><font style='color:red'>(", orderdetail.Num, "件)</font>" }));
                                builder2.Append("</a>)<br>");
                            }
                            builder2.Append("</td>");
                        }
                        if (info.GetModelList("Id=" + this.ordermodel.User_id).Count <= 0)
                        {
                            builder2.Append("<td>&nbsp;</td>");
                        }
                        else
                        {
                            foreach (Maticsoft.Model.UserInfo info2 in info.GetModelList("Id=" + this.ordermodel.User_id))
                            {
                                builder2.Append(string.Concat(new object[] { "<td><a href='/ajax/manage.aspx?action=userview&id=", this.ordermodel.User_id, "' class='ajaxlink'>", info2.Email, "<br/>", info2.Username, "</a>&nbsp;&raquo;&nbsp;<a href='/ajax/misc.aspx?action=sms&v=", info2.Mobile, "' class='ajaxlink'>短信</a></td>" }));
                            }
                        }
                        builder2.Append("<td>" + this.ordermodel.Quantity + "</td>");
                        builder2.Append("<td><span class='money'>\x00a5</span>" + this.ordermodel.Origin + "</td>");
                        switch (this.ordermodel.Service.ToLower())
                        {
                            case "yeepay":
                                builder2.Append("<td><span class='money'>&nbsp;</span>易宝</td>");
                                break;

                            case "alipay":
                                builder2.Append("<td><span class='money'>&nbsp;</span>支付宝</td>");
                                break;

                            case "tenpay":
                                builder2.Append("<td><span class='money'>&nbsp;</span>财富通</td>");
                                break;

                            case "chinabank":
                                builder2.Append("<td><span class='money'>&nbsp;</span>网银在线</td>");
                                break;

                            case "credit":
                                builder2.Append("<td><span class='money'>&nbsp;</span>余额付款</td>");
                                break;

                            case "cash":
                                builder2.Append("<td><span class='money'>&nbsp;</span>线下支付</td>");
                                break;

                            case "chinamobilepay":
                                builder2.Append("<td><span class='money'>&nbsp;</span>中国移动</td>");
                                break;

                            default:
                                builder2.Append("<td><span class='money'>&nbsp;</span>线下支付</td>");
                                break;
                        }
                        builder2.Append("<td><span class='money'>\x00a5</span>" + this.ordermodel.Credit + "</td>");
                        builder2.Append("<td><span class='money'>\x00a5</span>" + this.ordermodel.Money + "</td>");
                        if (this.ordermodel.Express.ToUpper() == "N")
                        {
                            builder2.Append("<td>优惠券</td>");
                        }
                        else if (this.ordermodel.Express_id != 0)
                        {
                            if (this.ordermodel.Express_id != 0)
                            {
                                Maticsoft.Model.Category category2 = new Maticsoft.BLL.Category().GetModel(this.ordermodel.Express_id);
                                if (category2 != null)
                                {
                                    builder2.Append("<td>" + category2.Name + "<br>");
                                    builder2.Append("</td>");
                                }
                                else
                                {
                                    builder2.Append("<td></td>");
                                }
                            }
                            else
                            {
                                builder2.Append("<td>快递</td>");
                            }
                        }
                        else
                        {
                            builder2.Append("<td></td>");
                        }
                        if (this.ordermodel.State == "pay")
                        {
                            builder2.Append(string.Concat(new object[] { "<td class='op' nowrap><a href='/ajax/manage.aspx?action=orderview&orderview=", row["Id"], "&pagenum=", this.pagenum, "&y=", base.Request["y"], "' class='ajaxlink'>详情</a></td>" }));
                        }
                        else
                        {
                            builder2.Append("<td class='op' nowrap><a href='#'></a></td>");
                        }
                        builder2.Append("</tr>");
                        num++;
                    }
                }
            }
            this.AspNetPager1.RecordCount = pagers.TotalRecords;
            this.pages = this.pagenum;
            this.Literal1.Text = builder.ToString();
            this.Literal2.Text = builder2.ToString();
        }

        public int pages
        {
            get
            {
                if (this.ViewState["pages"] != null)
                {
                    return Convert.ToInt32(this.ViewState["pages"].ToString());
                }
                return 1;
            }
            set
            {
                this.ViewState["pages"] = value;
            }
        }

        public string sqlWhere
        {
            get
            {
                if (this.ViewState["sqlWhere"] != null)
                {
                    return this.ViewState["sqlWhere"].ToString();
                }
                return " Express='Y' and Express_id>0 and state='pay' and len(express_no)>0";
            }
            set
            {
                this.ViewState["sqlWhere"] = value;
            }
        }
    }


